<template>
  <div class="v-container">
    <v-header title="完善信息">
      <span class="back" @click="goBack">
        <i class="el-icon-arrow-left"></i>
      </span>
    </v-header>
    <div class="mt">
      <div class="login-box">
        <div class="login-title">为了给您更好的服务，请完善以下信息：</div>
        <ul class="itemList" style="padding-left: 0px">
          <li>
            <mt-field label="姓名:" class="iconImg" placeholder="请输入车主姓名"  v-model="userName">
            </mt-field>
          </li>
          <li>
            <mt-field label="车牌号:" class="iconImg" placeholder="请输入完整的车牌号" v-model="carId">
            </mt-field>
          </li>
        </ul>

        <p style="font-size: 1.6rem;color: #ED6E39;margin: 10px">*以上信息作为权益使用人的信息，与手机号进行绑定，无法修改，请核实后填写</p>
      </div>
      <div class="button-box" v-on:click="submit" style="margin:30px">
        <p>保存</p>
      </div>
    </div>
  </div>
</template>

<script>
import vHeader from "@/components/Header.vue";
import countDown from '@/components/CountDown.vue';
import { Toast } from 'mint-ui';
import { Resource } from '@/services/resource';
import LocalStorage from '@/services/storage';

export default {
  name: "login",
  components: {
    vHeader,
     countDown
  },
  props: {
    title: {
      type: String,
      default: "标题"
      
    }
  },
  data() {
    return {
      userName: '',
      carId: ''
    };
  },
  methods:{
    // 返回上一页
    goBack(){
      this.$router.go(-1);
    },
    submit(){
      let express = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
      if (this.userName === '') {
        Toast({
          message: '用户名不能为空',
          position: 'middle'
        });
      } else if (this.carId.length !== 7 || this.carId == '') {
        Toast({
          message: '车牌号不能为空且只能输入七位',
          position: 'middle'
        });
      } else if ( !express.test(this.carId)) {
        Toast({
          message: '车牌号码不符合要求',
          position: 'middle'
        });
      } else {
        let loginStatus = LocalStorage.getLocalStorage('loginStatus');
        Resource.fillUserInfo.post({}, { matrixContent: loginStatus, userName: this.userName, carId: this.carId}).then(res => {
          console.log(res)
          if (res.head.resultCode == '000000') {
              Resource.userInfo
                .post({}, { matrixContent: loginStatus,needToken:true })
                .then(res => {
                   LocalStorage.setLocalStorage(
                    "userInfo",
                    JSON.stringify(res.body),
                    90 * 24 * 60 * 60 * 1000
                  );
                  if (res.head.resultCode == "000000") {
                    if(res.body.serviceEndTime == '' || res.body.serviceEndTime == null) {
                      this.$router.replace('/');
                    } else {
                      this.$router.replace('/mainpage');
                    }
                  }
                });
          }else{
            Toast({
              message: res.head.resultMsg,
              position: 'middle'
            });
          }
          
        });
      }
    }
  }
};
</script>

<style lang="less" scoped>
@import url("./login.less");

</style>
<style scoped>
  li >>> .mint-field-other{
    position: static;
  }
</style>